package com.qingyun.customview.draw.demo

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.ViewGroup
import android.widget.Scroller

class MyScrollViewGroup @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0,
) : ViewGroup(context, attrs, defStyleAttr) {
    private var mScreenHeight: Int
    private var mScreenWidth: Int
    private var mScroller = Scroller(context)

    init {
        mScreenHeight = getScreenHeight()
        mScreenWidth = getScreenWidth()
    }

    override fun computeScroll() {
        super.computeScroll()
        if (mScroller.computeScrollOffset()) {
            val currY = mScroller.currY
            scrollTo(0, currY)
            invalidate()
        }
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        val count = childCount
        for (i in 0 until count) {
            val child = getChildAt(i)
            measureChild(child, widthMeasureSpec, heightMeasureSpec)
        }
        setMeasuredDimension(mScreenWidth, mScreenHeight * count)
    }

    override fun onLayout(
        changed: Boolean,
        l: Int,
        t: Int,
        r: Int,
        b: Int,
    ) {
        val count = childCount
        (layoutParams as? MarginLayoutParams)?.let {
            it.height = mScreenHeight * count
            layoutParams = it
        }
        for (i in 0 until count) {
            val child = getChildAt(i)
            child.layout(0, i * mScreenHeight, r, (i + 1) * mScreenHeight)
        }
    }

    private var mLastY = 0
    private var mStartScrollY = 0
    override fun onTouchEvent(event: MotionEvent): Boolean {
        val y1 = event.y.toInt()
        when (event.action) {
            MotionEvent.ACTION_DOWN -> {
                // 拦截事件
                mLastY = y1
                mStartScrollY = scrollY
            }

            MotionEvent.ACTION_MOVE -> {
                // 处理事件
                if (!mScroller.isFinished) {
                    mScroller.abortAnimation()
                }
                var offsetY = mLastY - y1
                if (scrollY < 0) {
                    offsetY = 0
                }
                if (scrollY > height - mScreenHeight) {
                    offsetY = 0
                }
                scrollBy(0, offsetY)
                mLastY = y1
            }

            MotionEvent.ACTION_UP -> {
                val endScrollY = scrollY
                val dScrollY = endScrollY - mStartScrollY
                if (dScrollY > 0) {
                    if (dScrollY < mScreenHeight / 3) {
                        mScroller.startScroll(0, scrollY, 0, -dScrollY)
                    } else {
                        mScroller.startScroll(0, scrollY, 0, mScreenHeight - dScrollY)
                    }
                } else {
                    if (-dScrollY < mScreenHeight / 3) {
                        mScroller.startScroll(0, scrollY, 0, -dScrollY)
                    } else {
                        mScroller.startScroll(0, scrollY, 0, -mScreenHeight - dScrollY)
                    }
                }
            }
        }
        return true
    }

    private fun getScreenHeight(): Int {
        return context.resources.displayMetrics.heightPixels
    }

    private fun getScreenWidth(): Int {
        return context.resources.displayMetrics.widthPixels
    }
}